package com.A1908.service.impl;

import com.A1908.dao.impl.DataAnalysisDaoImpl;
import com.A1908.entity.*;

import java.util.ArrayList;
import java.util.List;

public class DataAnalysisServiceImpl {
    public TotalDataAnalysis totalData(User user) {
        TotalDataAnalysis totalDataAnalysis = new TotalDataAnalysis();
        DataAnalysisDaoImpl dao = new DataAnalysisDaoImpl();

        totalDataAnalysis.setPie(dao.getPieDetailInfo(user));//        扇形图的数据
        totalDataAnalysis.setTree(dao.getTreeDetailFeastInfo(user));//        柱状图的数据
        totalDataAnalysis.setLineDates(dao.getlineDataAnalyses(user));//查询折线图的x轴的时间，升序排列
        totalDataAnalysis.setLineRelationDataList(dao.getLineRelationData(user));//折线图的关系类型
        List<LineTotalData> total = dao.getLineTotal(user);//查询到的是从数据库查过来的折线图没有进行分组的根据时间降序排列的数据
        List<LineTotalList> totalLists = new ArrayList<>();//折线图的实体类

        //根据折线图的关系类型list的size创建size个lineTotalList实体类对象，并add到totalLists中，此时totalLists中的属性没有被赋值
        //初始化模板
        for (int i = 0; i < totalDataAnalysis.getLineRelationDataList().size(); i++) {
            LineTotalList lineTotalList = new LineTotalList();
            List<LineCountList> lineCountListList = new ArrayList<>();
            for (int j = 0; j < totalDataAnalysis.getLineDates().size(); j++) {
                LineCountList list = new LineCountList();
                list.setCount(0);
                lineCountListList.add(list);
            }
            List<LineDate> lineDates = new ArrayList<>();
            lineTotalList.setList(lineCountListList);
            lineTotalList.setDates(lineDates);
            totalLists.add(lineTotalList);
        }
        //对查询到的所有数据依次判断关系类型
        //根据条件对模板赋值
        for (int i = 0; i < total.size(); i++) {
            for (int k = 0; k < totalDataAnalysis.getLineRelationDataList().size(); k++) {
                //对查询到的数据根据关系类型进行分类
                if (total.get(i).getRelationID() == totalDataAnalysis.getLineRelationDataList().get(k).getRelationID()) {
                    //分类过后对该关系类型的totalLists进行赋值
                    totalLists.get(k).setRelationID(total.get(i).getRelationID());
                    LineCountList lineCountList = new LineCountList();
                    LineDate lineDate = new LineDate();
                    lineDate.setAttendData(total.get(i).getAttendDate());
                    totalLists.get(k).getDates().add(lineDate);
                    //让出席时间和当时的数据对应起来，解决了数据和时间不对应
                    for (int j = 0; j < totalDataAnalysis.getLineDates().size(); j++) {
                        //取得当前数据的时间在时间list中的下标（就是循环中的int j）
                        if (total.get(i).getAttendDate().equals(totalDataAnalysis.getLineDates().get(j).getAttendData())) {
                            //根据取得的下标j，给对应的关系类型的的对应时间进行赋值
                            totalLists.get(k).getList().get(j).setCount(total.get(i).getLineCount());
                            break;
                        }
                    }
                    break;
                }
            }
        }
        totalDataAnalysis.setLineTotalLists(totalLists);
        return totalDataAnalysis;
    }
}
